//
//  UpdateCarDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/13.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "UpdateCarDetViewController.h"
#import "CarDetCell.h"
#import "AddCarManaView.h"
#import "MapSelPlaceViewController.h"
#import "HouseImagesViewController.h"
#import "SelectHouseTypeView.h"

@interface UpdateCarDetViewController ()<UITableViewDelegate,UITableViewDataSource,MapSelPlaceViewControllerDelegate,UIPickerViewDelegate,UIPickerViewDataSource,HouseImagesViewControllerDelegate,BMKLocationServiceDelegate>{
    BMKLocationService *locationService;
    
    NSMutableArray *array;
    NSDictionary *dic;
    
    AddCarManaView *views;
    UIView *view1;
    UIView *view2;
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *arr1;//车辆类型
@property (nonatomic,copy)NSString *ImgUrl;//轮播
@property (nonatomic,strong)NSMutableArray *arrImgUrl;//轮播数组

@property (nonatomic,copy)NSString *strid;//车辆性质
@property (nonatomic,copy)NSString *lat;
@property (nonatomic,copy)NSString *lng;
@property (nonatomic,copy)NSString *province;
@property (nonatomic,copy)NSString *city;
@property (nonatomic,copy)NSString *area;


@end

@implementation UpdateCarDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setBGColor];
    [self showNav:@"修改商品"];
    array = [NSMutableArray array];
    _arr1 = [NSMutableArray array];
    _arrImgUrl = [NSMutableArray array];
    [self loadData];
    [self loadData2];
    
    [self loadLocal];
}

- (void)loadLocal{
    locationService = [[BMKLocationService alloc] init];
    locationService.delegate = self;
    //开启定位
    [locationService startUserLocationService];
    
}
#pragma -mark BMKLocationServiceDelegate
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    NSLog(@"lat=%f,lng=%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    
    CGFloat lat = userLocation.location.coordinate.latitude;
    CGFloat lng = userLocation.location.coordinate.latitude;
    _lat = [NSString stringWithFormat:@"%f",lat];
    _lng = [NSString stringWithFormat:@"%f",lng];
    [locationService stopUserLocationService];
}


- (void)loadUI{
    views = [[[NSBundle mainBundle] loadNibNamed:@"AddCarManaView" owner:nil options:nil] lastObject];
    views.frame = CGRectMake(0, 0, ScreenWidth, 830);
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-50) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = views;
    
    NSString *carthemephoto = [dic objectForKey:@"carthemephoto"];
    NSArray *arr = [carthemephoto componentsSeparatedByString:@"|"];
    NSMutableArray *images = [NSMutableArray array];
    for (NSString *aaa in arr) {
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],aaa];
        [images addObject:imgUrl];
    }
    
    views.image1.hidden = YES;
    views.labZP.hidden = YES;
    views.labZY.hidden = YES;
    UIView *v = [WTImageScroll ShowNetWorkImageScrollWithFream:CGRectMake(0, 0, ScreenWidth, 200) andImageArray:images andBtnClick:^(NSInteger tagValue) {
        HouseImagesViewController *house = [[HouseImagesViewController alloc] init];
        house.delegate = self;
        [self.navigationController pushViewController:house animated:YES];
    }];
    [views.views addSubview:v];
    
    views.text1.text = [dic objectForKey:@"brand"];
    NSString *type = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"type"] integerValue]];
    if ([type integerValue] == 1) {
        views.lab1.text = @"准新车";
    }else{
        views.lab1.text = @"新车";
    }
    _ImgUrl = carthemephoto;
    
    _province = [dic objectForKey:@"province"];
    _city = [dic objectForKey:@"city"];
    _area = [dic objectForKey:@"area"];
    
    views.text2.text = [dic objectForKey:@"sellingprice"];
    views.text3.text = [dic objectForKey:@"guideprice"];
    views.text4.text = [dic objectForKey:@"carcolor"];
    views.text5.text = [dic objectForKey:@"customerservice"];
    views.lab2.text = views.lab1.text;
    views.text6.text = [dic objectForKey:@"travel"];
    
    views.lab3.text = [dic objectForKey:@"thefactorytime"];
    views.lab4.text = [dic objectForKey:@"address"];
    views.text7.text = [dic objectForKey:@"detailedaddress"];
    
    _strid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"type"] integerValue]];
    //新车标签
    views.lab1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click1)];
    [views.lab1 addGestureRecognizer:tap1];
    //选择日期
    views.lab3.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click3)];
    [views.lab3 addGestureRecognizer:tap3];
    //地图
    views.lab4.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click4)];
    [views.lab4 addGestureRecognizer:tap4];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, ScreenHeight-50, ScreenWidth, 50);
    btn.backgroundColor = COLOR_RGB(253, 17, 66, 1);
    [btn setTitle:@"修改" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickUpdate) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}
//修改商品
- (void)clickUpdate{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_ImgUrl forKey:@"photo"];
    [parmas setObject:_productId forKey:@"id"];
    [parmas setObject:views.text1.text forKey:@"brand"];
    [parmas setObject:[dic objectForKey:@"model"] forKey:@"model"];
    [parmas setObject:_strid forKey:@"type"];
    [parmas setObject:views.text2.text forKey:@"guideprice"];
    [parmas setObject:views.text3.text forKey:@"sellingprice"];
    [parmas setObject:views.text4.text forKey:@"carcolor"];
    [parmas setObject:views.text5.text forKey:@"customerservice"];
    [parmas setObject:views.text6.text forKey:@"travel"];
    [parmas setObject:views.lab3.text forKey:@"thefactorytime"];
    [parmas setObject:_province forKey:@"province"];
    [parmas setObject:_city forKey:@"city"];
    [parmas setObject:_area forKey:@"area"];
    [parmas setObject:views.lab4.text forKey:@"detailedaddress"];
    NSString *str = [array componentsJoinedByString:@"|"];
    [parmas setObject:str forKey:@"detailsa"];
    [parmas setObject:_lng forKey:@"longitude"];
    [parmas setObject:_lat forKey:@"latitude"];
    [parmas setObject:views.text7.text forKey:@"address"];
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/CarBuying/update",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"修改商品res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

- (void)click1{
    view1 = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-250, ScreenWidth, 250)];
    view1.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view1];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(10, 0, 40, 40);
    [btn1 setTitle:@"取消" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(ScreenWidth-50, 0, 40, 40);
    [btn2 setTitle:@"确定" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(clickSure) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:btn2];
    
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 210)];
    pickerView.dataSource = self;
    pickerView.delegate = self;
    [view1 addSubview:pickerView];
}

- (void)clickCancel{
    [view1 removeFromSuperview];
    [view2 removeFromSuperview];
}

- (void)clickSure{
    [view1 removeFromSuperview];
    [view2 removeFromSuperview];
}
//
- (void)click3{
    view2 = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-250, ScreenWidth, 250)];
    view2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view2];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(10, 0, 40, 40);
    [btn1 setTitle:@"取消" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(ScreenWidth-50, 0, 40, 40);
    [btn2 setTitle:@"确定" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(clickSure) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:btn2];
    
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 210)];
    datePicker.backgroundColor = [UIColor grayColor];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];
    datePicker.date = [NSDate date];
    [view2 addSubview:datePicker];
    //添加事件
    [datePicker addTarget:self action:@selector(selectDate:) forControlEvents:UIControlEventValueChanged];
}

- (void)selectDate:(id)sender{
    UIDatePicker *picker = (UIDatePicker *)sender;
    NSDate *date = picker.date;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSString *str = [df stringFromDate:date];
    views.lab3.text = str;
    NSLog(@"str:%@",str);
    
}
//选择地址
- (void)click4{
    MapSelPlaceViewController *place = [[MapSelPlaceViewController alloc] init];
    place.delegate = self;
    [self.navigationController pushViewController:place animated:YES];
}

-(void)sendImgUrl:(NSMutableArray *)imgUrl{
    _arrImgUrl = imgUrl;
    _ImgUrl = [_arrImgUrl componentsJoinedByString:@"|"];
}

#pragma -mark MapSelPlaceViewControllerDelegate
-(void)sendLat:(NSString *)lat sendLng:(NSString *)lng{
    _lat = lat;
    _lng = lng;
}

-(void)sendText:(NSString *)text Province:(NSString *)proce City:(NSString *)city Area:(NSString *)area{
    views.lab4.text = [NSString stringWithFormat:@"%@,%@,%@",proce,city,area];
    views.text7.text = text;
    _province = proce;
    _city = city;
    _area = area;
}
#pragma -mark UIPickerViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _arr1.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *str = [_arr1[row] objectForKey:@"name"];
    return str;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSString *str = [_arr1[row] objectForKey:@"name"];
    views.lab1.text = str;
    views.lab2.text = views.lab1.text;
    _strid = [_arr1[row] objectForKey:@"id"];
    NSLog(@"str:%@",str);
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    CarDetCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CarDetCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *op = array[indexPath.row];
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[op objectForKey:@"img"]];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@""]];
    cell.text.text = [op objectForKey:@"details"];
    [cell.text setEditable:NO];
    cell.text.backgroundColor = [UIColor grayColor];
    
    cell.imgPC.hidden = YES;
    cell.labSC.hidden = YES;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 350.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(16, 10, ScreenWidth-32, 30);
    [btn setTitle:@"+  添加更多图文说明" forState:UIControlStateNormal];
    [btn setTitleColor:COLOR_RGB(17, 100, 253, 1) forState:UIControlStateNormal];
    btn.layer.cornerRadius = 5.0;
    btn.layer.borderWidth = 1.0;
    btn.layer.borderColor = COLOR_RGB(17, 100, 253, 1).CGColor;
    [btn addTarget:self action:@selector(clickAdd) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40.0;
}

- (void)clickAdd{
    
}

//车子详情
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_productId forKey:@"id"];
    [parmas setObject:@"0" forKey:@"longitude"];
    [parmas setObject:@"0" forKey:@"latitude"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/CarBuying/details",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"车辆详情res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            dic = [responseObject objectForKey:@"result"];
            NSArray *arr = [dic objectForKey:@"detailsa"];
            if ([arr isKindOfClass:[NSArray class]]) {
                array = [arr mutableCopy];
            }
            [_tableView reloadData];
            [self loadUI];
        }
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

//购车-汽车性质
- (void)loadData2{
    NSString *url = [NSString stringWithFormat:@"%@/App/CarBuying/type",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"购车res:%@",responseObject);
        _arr1 = [responseObject objectForKey:@"result"];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
